/**初始化**/
var sessionUser,form,layer;
layui.use(['layer','laydate','form','table'], function() {
	sessionUser = $('#sessionUser').val();
	var laydate = layui.laydate;
	form = layui.form;
	//货单事件初始化
	laydate.render({
		elem: '#dayTime',
		theme: 'molv',
		showBottom: false,
		calendar: true,
		trigger: 'click',
		value: new Date(),
		change: function(value, date){
			lay('#dayTime').html(value);
		}
	});
	laydate.render({
		elem: '#payTime',
		theme: 'molv',
		showBottom: false,
		calendar: true,
		trigger: 'click',
		value: new Date(),
		change: function(value, date){
			lay('#payTime').html(value);
		}
	});
	
	//queryTypeNameSelect();
});

/**
 *	保存数据
 */
function save(){
	$('#typeCode0').attr("disabled",false);
	var params = $('#accountForm').serializeArray();
	$.ajax({
		url : "/account/goods/save",
		type : "post",
		data : params,
		beforeSend : function () {
        	this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
      	},
		success : function(data) {
			layer.close(this.layerIndex);
			if (data.success) {
				 window.location.reload();
			}else{
				layer.msg('操作失败!', {icon: 5});
			}
		},
		complete : function () {
			layer.close(this.layerIndex);
		}
	})
}



//增加行
var k = 1, j = 1;
function addTr(e){
	var html;
	if (e == 1) {
		html ='<tr>'
				+'<td>'
				+'	<input type="hidden" name="index" id="index' + k + '">'
				+'	<input type="text" name="goodsSource[' + k + ']" id="goodsSource' + k + '" required lay-verify="required" placeholder="拿货来源" autocomplete="off" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="goodsNumber[' + k + ']" id="goodsNumber' + k + '" required lay-verify="required" placeholder="款号编码" autocomplete="off" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="goodsColor[' + k + ']" id="goodsColor' + k + '" required lay-verify="required" placeholder="颜色" autocomplete="off" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="goodsSize[' + k + ']" id="goodsSize' + k + '" required lay-verify="required" placeholder="尺码大小" autocomplete="off" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="inPrice[' + k + ']" id="inPrice' + k + '" required lay-verify="required" placeholder="价格(元)" onchange="valiteNumber(this)" onblur="getSumPrice(' + k + ')" autocomplete="off" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="number[' + k + ']" id="number' + k + '" required lay-verify="required" placeholder="数量" onchange="valiteNumber(this)" onblur="getSumPrice(' + k + ')" autocomplete="off" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="sumPrice[' + k + ']" id="sumPrice' + k + '" required lay-verify="required" placeholder="总额" onchange="valiteNumber(this)" autocomplete="off" class="layui-input" readonly="readonly">'
				+'</td>'
				+'<td>'
				+'<input type="hidden" name="goodsTypeCode" value="QH">'
				+'<input type="text" class="layui-input" readonly="readonly" value="取货">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="goodsConfirmPerson[' + k + ']" id="goodsConfirmPerson' + k + '"  required lay-verify="required" placeholder="确认人" autocomplete="off" value="'+ sessionUser +'" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<input type="text" name="goodsRemake[' + k + ']" id="goodsRemake' + k + '" required lay-verify="required" placeholder="备注" autocomplete="off" class="layui-input">'
				+'</td>'
				+'<td>'
				+'	<div class="layui-inline" onclick="addTr(1)">'
				+'		<i class="layui-icon layui-icon-add-circle-fine"></i>'
				+'	</div>'
				+'	<div class="layui-inline" onclick="deleteTr($(this))">'
				+'		<i class="layui-icon layui-icon-delete"></i>'
				+'	</div>'
				+'</td>'
				+'</tr>';
		$('#tb_goods').append(html);
		$('#td0').children().hide();
		
		k++;
	}
	
	if (e == 2) {
		html = '<tr>'
			+'<td>'
			+'	<select name="typeCode[' + j + ']" id="typeCode' + j + '"></select>'
			+'</td>'
			+'<td>'
			+'	<input type="text" name="payPrice[' + j + ']" id="payPrice' + j + '" onblur="getSumPrice()" required lay-verify="required" onchange="valiteNumber(this)" placeholder="支出金额(元)" autocomplete="off" class="layui-input">'
			+'</td>'
			+'<td>'
			+'	<input type="text" name="describe[' + j + ']" id="describe' + j + '" required lay-verify="required" placeholder="描述" autocomplete="off" class="layui-input">'
			+'</td>'
			+'<td>'
			+'	<input type="text" name="detailConfirmPerson[' + j + ']" id="detailConfirmPerson' + j + '" required lay-verify="required" value="'+ sessionUser +'" placeholder="确认人" autocomplete="off" class="layui-input">'
			+'</td>'
			+'<td>'
			+'	<input type="text" name="detailRemake[' + j + ']" id="detailRemake' + j + '" required lay-verify="required" placeholder="备注" autocomplete="off" class="layui-input">'
			+'</td>'
			+'<td>'
			+'	<div class="layui-inline" onclick="addTr(2)">'
			+'		<i class="layui-icon layui-icon-add-circle-fine"></i>'
			+'	</div>'
			+'	<div class="layui-inline" onclick="deleteTr($(this))">'
			+'		<i class="layui-icon layui-icon-delete"></i>'
			+'	</div>'
			+'</td>'
			+'</tr>';
		queryTypeNameSelect(j);	
		$('#tb_account_detail').append(html);
		$('#td1').children().hide();
		
		j++;
	}
};

//删除行
function deleteTr(e){
	e.parent().parent().remove();
	if ($('#goods_info').children().length == 1) {
		$('#td0').children().show();
	}
	if ($('#account_detail_info').children().length == 1) {
		$('#td1').children().show();
	}
	getSumPrice();
}

/**
 * 计算总额
 */
function getSumPrice(e){
	
	var sumPrice = 0,sumPayPrice=0,paySumPrice=0;
	var inPrice = $('#inPrice' + e).val();
	var number = $('#number'+ e).val();
	
	if ((inPrice != undefined && inPrice != '' && inPrice != null) && (number != undefined && number != '' && number != null)) {
		//出货总额赋值
		$('#sumPrice'+e).val(inPrice * number);
	}
	
	$('#tb_goods tr').each(function(){
		$(this).find('td:eq(6)').each(function(){
			sumPayPrice += parseFloat($(this).children().val());
		})
	});
	
	// 明细表 类型为取货时 支出金额赋值
	if (sumPayPrice != undefined && sumPayPrice != '' && sumPayPrice != null && !isNaN(sumPayPrice)) {
		$('#payPrice0').val(sumPayPrice);
	}
	
	// 计算汇总表总金额
	$('#tb_account_detail tr').each(function(){
		$(this).find('td:eq(1)').each(function(){
			paySumPrice += parseFloat($(this).children().val());
		})
	});
	
	if (paySumPrice != undefined && paySumPrice != '' && paySumPrice != null && !isNaN(paySumPrice)) {
		$('#paySumPrice').val(paySumPrice);
	}
}

// 查询支出类型下拉框数据
function queryTypeNameSelect(e){
	 $.ajax({
		url: '/account/query/type/name',
		type: 'post',
		dataType: 'json',
		success: function(data){
			var optionData = '<option value="">请选择</option>';
			$.each(data, function(i,item){
				optionData += '<option value="' + item.typeCode + '">' + item.typeName + '</option>';
			});
			
			$('#typeCode' + e).empty();
			$('#typeCode' + e).html(optionData);
        	form.render('select');
		}
	});
}

// 金额为数字
function valiteNumber(obj){
	var validAmount = $(obj).val();
    var valiteAmount = /^(([1-9]\d*)|\d)(\.\d{1,2})?$/;
	if(validAmount != null && validAmount != '' && valiteAmount.test(validAmount)==false){
    	layer.alert('必须为数字格式!', {icon: 2});
		$(obj).val();	
    	return;
   }
}